/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test;

import java.util.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.util.zip.*;

/**
 *
 * @author britt
 */
public class PersistentHash {

    public PersistentHash() {
        DMTools.characterHash = new Hashtable();
        DMTools.monsterHash   = new Hashtable();
        DMTools.effectHash    = new Hashtable();
        DMTools.powerHash     = new Hashtable();
    }

    public void saveFiles() {
        try {
            System.out.println("Creating save files");
            FileOutputStream   mfileOut = new FileOutputStream("monster");
            GZIPOutputStream   mGZ      = new GZIPOutputStream(mfileOut);
            ObjectOutputStream mGZout   = new ObjectOutputStream(mGZ);
            FileOutputStream   cfileOut = new FileOutputStream("character");
            GZIPOutputStream   cGZ      = new GZIPOutputStream(cfileOut);
            ObjectOutputStream cGZout   = new ObjectOutputStream(cGZ);
            FileOutputStream   efileOut = new FileOutputStream("effect");
            GZIPOutputStream   eGZ      = new GZIPOutputStream(efileOut);
            ObjectOutputStream eGZout   = new ObjectOutputStream(eGZ);
            FileOutputStream   pfileOut = new FileOutputStream("power");
            GZIPOutputStream   pGZ      = new GZIPOutputStream(pfileOut);
            ObjectOutputStream pGZout   = new ObjectOutputStream(pGZ);

            System.out.println("Writing Hashtables");
            mGZout.writeObject(DMTools.monsterHash);
            cGZout.writeObject(DMTools.characterHash);
            eGZout.writeObject(DMTools.effectHash);
            pGZout.writeObject(DMTools.powerHash);

            System.out.println("Closing all output streams");
            mGZout.close();
            cGZout.close();
            eGZout.close();
            pGZout.close();

        } catch (FileNotFoundException f) {
            f.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void loadFiles() {
        try {
            System.out.println("Loading save files");
            FileInputStream   mfileIn = new FileInputStream("monster");
            GZIPInputStream   mGZ     = new GZIPInputStream(mfileIn);
            ObjectInputStream mGZin   = new ObjectInputStream(mGZ);
            FileInputStream   cfileIn = new FileInputStream("character");
            GZIPInputStream   cGZ     = new GZIPInputStream(cfileIn);
            ObjectInputStream cGZin   = new ObjectInputStream(cGZ);
            FileInputStream   efileIn = new FileInputStream("effect");
            GZIPInputStream   eGZ     = new GZIPInputStream(efileIn);
            ObjectInputStream eGZin   = new ObjectInputStream(eGZ);
            FileInputStream   pfileIn = new FileInputStream("power");
            GZIPInputStream   pGZ     = new GZIPInputStream(pfileIn);
            ObjectInputStream pGZin   = new ObjectInputStream(pGZ);

            System.out.println("Reading Hashtables");
            DMTools.monsterHash   = (Hashtable) mGZin.readObject();
            DMTools.characterHash = (Hashtable) cGZin.readObject();
            DMTools.effectHash    = (Hashtable) eGZin.readObject();
            DMTools.powerHash     = (Hashtable) pGZin.readObject();

            System.out.println("Closing all input streams");
            mGZin.close();
            cGZin.close();
            eGZin.close();
            pGZin.close();

        } catch (ClassNotFoundException c) {
            c.printStackTrace();
        } catch (FileNotFoundException f) {
            System.out.println("Objects not yet initialized");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
